<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/23
 * Time: 19:44
 */

namespace App\Services\Msg;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Msg\MsgCount;
use App\Models\Msg\Msglist;
use App\Models\User\UserModel;

class MsgService
{
    private $uidList = [];

    public function adduid($uid)
    {
        $this->uidList[] = $uid;
    }

    private $talk = '';

    private $list = [];

    public function load()
    {
        sort($this->uidList);
        $this->talk = implode('_', $this->uidList);
        // $this->talk = md5($this->talk);

    }

    private $maxid = 0;

    public function getmaxid()
    {
        return $this->maxid;
    }

    private $userlist = [];

    private function getUser($uid)
    {
        if (isset($this->userlist[$uid])) {
            return $this->userlist[$uid];
        }
        $user = UserModel::getinfo($uid);
        $this->userlist[$uid] = [
            'head' => $user['head'],
            'name' => $user['name']
        ];
        return $this->userlist[$uid];
    }

    public function getNewlist($maxid = 0)
    {
        $list = $this->list = Msglist::where([
            'talk' => $this->talk
        ])->where('id', '>', $maxid)
            ->select([
                'id', 'content', 'status', 'type', 'uid', 'created_at', 'other'
            ])
            ->orderBy('id', 'desc')
            ->skip(0)
            ->take(20)
            ->get();
        //   ->toArray();
        $this->maxid = $maxid;
        $array = [];
        $uid = OA::$user->getId();
        foreach ($list as $key => $vo) {
            if ($key == 0) {
                $this->maxid = $list[0]['id'];
            }
            // $list[$key]->created_at=Fun::date($list[$key]->created_at);
            if ($vo->other == OA::$user->getId()) {
                $vo->status = Msglist::status_yidu;
                $vo->save();
              //  $this->clearCount($vo->other, $vo->uid);
            }
            $array[] = [
                'id' => $vo->id,
                'content' => $vo->content,
                'status' => $vo->status,
                'type' => $vo->type,
                'uid' => $vo->uid,
                'created_at' => Fun::date($vo->created_at),
                'other' => $vo->other,
                'user' => $this->getUser($vo->uid),
                'isme' => $vo->uid == $uid
            ];
        }

        return $array;
    }


    /**
     * 添加消息
     */
    public function send($duifang, $text)
    {
        $new = new Msglist();
        $new->talk = $this->talk;
        $new->content = Fun::encode($text);
        $new->status = Msglist::status_weidu;
        $new->type = Msglist::type_text;
        $new->uid = OA::$user->getId();
        $new->created_at = time();
        $new->other = $duifang;
        $new->save();
        $this->upOtherCount($new->uid, $new->other);
        $this->upmytime($new->uid, $new->other);
    }

    /**
     * 返回用户列表 包括用户头像,名字,消息数量
     */
    public function getmsglist()
    {
        $list = MsgCount::where([
            'uid' => OA::$user->getId()
        ])->select([
            'count',
            'other',
            'time'
        ])
            ->orderBy('time', 'DESC')->limit(20)->get()->toArray();
        foreach ($list as $key => $vo) {
            $list[$key]['user'] = $this->getUser($vo['other']);
            $list[$key]['time'] = Fun::date($list[$key]['time']);
        }
        return $list;
    }

    /**
     * 累加对方未读的数量
     */
    private function upOtherCount($otheruid,$myuid)
    {

        $count = \DB::update(
            'update mc_msgcount set time=?,count=count+1 where uid = ? AND other=?', [
            time(),
            $myuid,
            $otheruid
        ]);

        if ($count == 0) {
            $new = new MsgCount;
            $new->uid = $myuid;
            $new->other = $otheruid;
            $new->count = 1;
            $new->time = time();
            $new->save();
        }
    }

    /**
     * 更新自己消息列表的位置
     */
    private function upmytime($myuid,$otheruid)
    {

        $count = \DB::update(
            'update mc_msgcount set time=? ,count=0 where uid = ? AND other=?', [
            time(),
            $myuid,
            $otheruid
        ]);

        if ($count == 0) {
            $new = new MsgCount;
            $new->uid = $myuid;
            $new->other = $otheruid;
            $new->count = 0;
            $new->time = time();
            $new->save();
        }
    }

    /**
     *获取未读消息的总数
     */
    public function getmsgUnreadCount()
    {
        $count = MsgCount::where([
            'uid' => OA::$user->getId()
        ])->select([
            'count'
        ])->sum('count');
        return $count;
    }


    /**
     * 清空未读的消息
     */
    public function clearCount($myuid, $otheruid)
    {
        MsgCount::where([
            'uid' => $myuid,
            'other' => $otheruid
        ])->update(['count'=>0]);
    }

}